package mju.cxf.config;


import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import mju.cxf.interceptor.JwtTokenInterceptor;
import mju.cxf.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
//import org.redisson.Redisson;
//import org.redisson.api.RedissonClient;
//import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
@EnableKnife4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry http拦截信息
     */
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册卡先锋TOKEN拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/user/**")
                .addPathPatterns("/admin/**")
                .addPathPatterns("/employee/**")
                .excludePathPatterns("/user/api/user/wxLogin")
                .excludePathPatterns("/user/api/email/getCode")
                .excludePathPatterns("/admin/api/admin/login")
                .excludePathPatterns("/employee/api/employee/login")
                .excludePathPatterns("/user/api/specifications/pages")
                .excludePathPatterns("/user/api/alipay/**");
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }



    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter c = new MappingJackson2HttpMessageConverter();
        c.setObjectMapper(new JacksonObjectMapper());//更改了
        converters.add(0, c);
    }
}
